(function($) {

	$.SJR = function(){};

	$.SJR.JSONENGINE = "1.0.0";

	var action_success = function(isFn, object1, object2, type, success){
		return function(response, status){
			success(isFn, object1, object2, type, response, status);
		}
	};

	var success = function(flg, mainobject, subobject, type, response, status){
		var input_success = function(response, status){
			if(type=='DELETE'){
				var o = {};
				for(var i in $(mainobject).values())
					o[i] = '';
				$(mainobject).values(o);
			}
			else{
				if(!$(mainobject).attr('class').match(/_jeClearForm/)){
					$(mainobject).values(response);
				} else {
					if(type!='GET'){
						var o = {};
						for(var i in $(mainobject).values())
							o[i] = '';
						$(mainobject).values(o);
					}
				}
			}
		};
		var output_success = function(response, status){
			mainobject.empty();
			var subobj = '#'+mainobject[0].id.slice(0,-5);
			for(var i in response){
				var div = $('<div/>');
				div.append(JSON.stringify(response[i]))
				div.click(function(){
					$(subobj).values(JSON.parse($(this).text()));
				});
				mainobject.append((div));
			}
			if($(subobject).attr('class').match(/_jeClearForm/)){
				var o = {};
				for(var i in $(subobject).values())
					o[i] = '';
				$(subobject).values(o);
			}
		};
		var _exec = (flg)? output_success:input_success;
		_exec(response, status);
	};
	var error = function(xhr, status, thrown){
		window.alert('error');
	};

	/**
	 * output: jsonengineから検索してきた一覧の表示先
	 * type: リクエストメソッド
	 * input: 入力フォーム
	 * input_success: 更新用$.ajax成功時のコールバック関数
	 * output_success: 一覧用$.ajax失敗時のコールバック関数
	 */
	$.SJR.ajaxbase_beta = function(output, type, input, input_success, output_success){
		var option = {}
		if(input[0]['_docId']){  //データ入力フォーム
			var url = '/_je/' + input[0].id;
			var i = input.values();
			if(type=='POST'){
				if(i._docId.trim()==''){
					delete i._docId;
				}
			}
			option.type = type;
			if(type!='DELETE'){
				option.url = url;
				option.data = i;
			}else{
				option.url = url + '/' + i._docId;
			}
			if(input_success)
				option.success = action_success(0, input, output, type, input_success);
			else
				option.success = action_success(0, input, output, type, success);
			option.error = error;
			$.ajax(option);
			//出力
			var option_for_list = {}
			option_for_list.type = 'GET';
			option_for_list.url = url;
			var cond = {};
			cond.sort = '_updatedAt.desc';
			option_for_list.data = cond;
			if(output_success)
				option_for_list.success = action_success(1, output, input, 'GET', output_success);
			else
				option_for_list.success = action_success(1, output, input, 'GET', success);
			option_for_list.error = error;
			$.ajax(option_for_list);
		} else {  //検索入力フォーム
			var url = '/_je/' + input[0].id.slice(0,-7);
			var src = input.values();
			var cond = '';
			for(var i in src){
				if(src[i].trim())
					cond += '&cond=' + i + '.eq.' + src[i]; 
			}
			cond += '&' + 'sort=_updatedAt.desc';
			option.type = type;
			option.url = url
			option.data = cond.substr(1,cond.length);
			if(input_success)
				option.success = action_success(1, output, input, type, input_success);
			else
				option.success = action_success(1, output, input, type, success);
			option.error = error;
			$.ajax(option);
		}
	};

	$.fn.extend({
		_POST : function(input, input_success, output_success) {
			$.SJR.ajaxbase_beta(this, "POST", input, input_success, output_success);
			return this;
		},
		_GET : function(input, input_success, output_success) {
			$.SJR.ajaxbase_beta(this, "GET", input, input_success, output_success);
			return this;
		},
		_POST : function(input, input_success, output_success) {
			$.SJR.ajaxbase_beta(this, "POST", input, input_success, output_success);
			return this;
		},
		_PUT : function(input, input_success, output_success) {
			$.SJR.ajaxbase_beta(this, "PUT", input, input_success, output_success);
			return this;
		},
		_DELETE : function(input, input_success, output_success) {
			$.SJR.ajaxbase_beta(this, "DELETE", input, input_success, output_success);
			return this;
		}
	});

})(jQuery);

